package csc.fresher_group2.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import csc.fresher_group2.dao.IFileDAO;
import csc.fresher_group2.dao.IFileDAOImpl;
import csc.fresher_group2.model.Account;
import csc.fresher_group2.model.FileInfo;

/**
 * Servlet implementation class TrashServlet
 * 
 * @author Phuc
 */
public class TrashServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TrashServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doActionFiles(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doActionFiles(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @author Phuc
	 * @param request
	 * @param response
	 */
	protected void doActionFiles(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Account user = (Account) session.getAttribute("account");
		String rootPath = getServletContext().getRealPath(
				"\\users\\" + user.getUserName());
		if (user == null) {
			response.sendRedirect("login.jsp");
		} else {
			String page = "trash.jsp";
			String action = request.getParameter("action");
			IFileDAO iFileDao = new IFileDAOImpl();
			if (action == null) {
				FileInfo[] fInfos = iFileDao.getFiles(rootPath + "\\trash");
				request.setAttribute("fInfos", fInfos);
			} else if (action.equals("delete")) {
				String[] files = request.getParameterValues("fileList");
				IFileDAO iFileDAO = new IFileDAOImpl();

				if (files != null) {
					for (int i = 0; i < files.length; i++) {
						iFileDao.deleteFile(new File(rootPath + "\\trash\\"
								+ files[i]));
					}
				}

				FileInfo[] fInfos = iFileDao.getFiles(rootPath + "\\trash");
				request.setAttribute("fInfos", fInfos);
			} else if (action.equals("restore")) {
				String[] files = request.getParameterValues("fileList");
				IFileDAO iFileDAO = new IFileDAOImpl();

				if (files != null) {
					for (int i = 0; i < files.length; i++) {
						File src = new File(rootPath + "\\trash\\" + files[i]);
						File des = new File(rootPath);
						iFileDao.moveFile(src.getPath(), des.getPath());
					}
				}

				FileInfo[] fInfos = iFileDao.getFiles(rootPath + "\\trash");
				request.setAttribute("fInfos", fInfos);
			}

			request.getRequestDispatcher(page).forward(request, response);
		}
	}
}
